package com.bookshop.sale.logic;

import java.util.ArrayList;

import com.bookshop.book.logic.Book;
import com.bookshop.book.logic.BookManagement;
import com.bookshop.dataOperator.DatabaseUtils;
import com.bookshop.tool.Format;
import com.bookshop.user.data.CustomerCommonData;
import com.bookshop.user.logic.CustomerInfo;
import com.bookshop.vo.BookVO;
import com.bookshop.vo.ValueObject;

/**
 * 
 * @author _Alpha
 */

public class CollectionManage {
	Collection mCollection;
	CustomerInfo mCustomer;

	public CollectionManage(CustomerInfo pCustomer) {
		mCollection = new Collection(pCustomer);
		mCustomer = pCustomer;
	}

	public ValueObject lookCollection() {
		BookVO bookVO = new BookVO();
		bookVO.update(mCollection.changeToArray(mCollection.mCollectedBooks));
		return bookVO;
	}

	public boolean remove(Book pBook) {
		return mCollection.remove(pBook);
	}

	public boolean add(Book pBook) {
		return mCollection.add(pBook);
	}

	public Book[] getupdatedBooks() {
		return mCollection.getUpdatedBooks();
	}

}

class Collection implements CustomerCommonData {
	ArrayList<Book> mCollectedBooks;
	CustomerInfo mCustomer;

	Collection(CustomerInfo pCustomer) {
		mCustomer = pCustomer;
		mCollectedBooks = new ArrayList<Book>();
		BookManagement bm = new BookManagement();
		Book[] collectBooks = new Book[getCollectionsID().length];
		for (int i = 0; i < collectBooks.length; i++) {
			collectBooks[i] = bm.searchByID(getCollectionsID()[i]);
		}
		for (int i = 0; i < collectBooks.length; i++) {
			mCollectedBooks.add(collectBooks[i]);
		}
	}

	boolean remove(Book pBook) {
		for (int i = 0; i < mCollectedBooks.size(); i++) {
			if (pBook.getId().equals(mCollectedBooks.get(i).getId())) {
				mCollectedBooks.remove(i);
			}
		}
		return updateBooks();
	}

	boolean add(Book pBook) {
		for (int i = 0; i < mCollectedBooks.size(); i++) {
			if(mCollectedBooks.get(i).getId().equals(pBook.getId())) return false;
		}
		
		boolean isSuceess = false;
		if (mCollectedBooks.size() < MAX_COLLECTION) {
			mCollectedBooks.add(pBook);
			isSuceess = updateBooks();
		}
		return isSuceess;
	}

	private boolean updateBooks() {
		String updateSql = "update " + CUSTOMER_TABLE + " set " + COLLECTIONS
				+ "='" + Format.formatBooks(mCollectedBooks) + "' where "
				+ ACCOUNT + "='" + mCustomer.getAccount() + "'";
		return DatabaseUtils.excute(updateSql);
	}

	Book[] getUpdatedBooks() {
		Book[] result = new Book[mCollectedBooks.size()];
		for (int i = 0; i < result.length; i++) {
			result[i] = mCollectedBooks.get(i);
		}
		return result;
	}

	private String[] getCollectionsID() {
		String idsStrings = ((String) DatabaseUtils.getSingleObject(CUSTOMER_TABLE, ACCOUNT,
				mCustomer.getAccount(), INDEX_BOOKS));
		if(idsStrings.equals("")){
			return new String[0];
		}
		return idsStrings.split(",");
	}

	Book[] changeToArray(ArrayList<Book> pBooks) {
		Book[] result = new Book[pBooks.size()];
		for (int i = 0; i < pBooks.size(); i++) {
			result[i] = pBooks.get(i);
		}
		return result;
	}
}
